<?php
/*
 * 停车费
 */
namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class FeesController extends Controller{

    public function Money(Request $request){
        if($request ->isMethod('post')){
            $user_id     = $request ->input('user_id');
            $data        = DB::table('parking_fees')
                ->select('parking_start','parking_end','parking_money','parking_set')
                ->where('user_parkingfees',$user_id)
                ->where('parking_state',0)
                ->get();
            foreach ($data as $value){
                $dd[]   = array(
                    'time'  =>$value ->parking_set,
                    'money' =>$value ->parking_money
                );
            }
            if(empty($dd)){
                $dats  = array();
            }else{
                $dats = $dd;
            }

            /*$Array             = (object)array();
            $start             = $request ->input('start');
            $end               = $request ->input('end');
            $village_id        = $request ->input('village_id');
            $time = ceil((strtotime($end)-strtotime($start))/86400);
            $select_time   = DB::table('village')
                ->where('village_id',$village_id)
                ->get();

            foreach ($select_time as $value){
                $id   = $value ->village_sum;
                $money   = $id*$time;
                $dd  = array('money' =>$money);
            }*/


            if($dats == true){
                return json::json('success', '停车费信息获取成功', $dats);
            }else{
                return json::json('failure', '停车费信息获取成功', []);
            }
        }

    }

    public function Fees(Request $request){
        if($request ->isMethod('post')){
            $Array             = (object)array();
           $user_id           = $request ->input('user_id');
            $start             = $request ->input('start');
            $end               = $request ->input('end');
            $parking_money     = $request ->input('money');
            $select            = DB::table('parking_fees')
                               ->where('user_parkingfees',$user_id)
                               ->where('parking_start',$start)
                               ->where('parking_end',$end)
                               ->where('parking_money',$parking_money)
                               ->get();
            if($select == true){
                return json::json('failure', '你提交的停车费订单已存在', $Array);
            }else{
                $data          = DB::table('parking_fees')
                               ->insert([
                                   'user_parkingfees' =>$user_id,
                                   'parking_start'    =>$start,
                                   'parking_end'      =>$end,
                                   'parking_money'    =>$parking_money
                               ]);
                if($data == true){
                    return json::json('success', '停车费交付成功', $Array);
                }else{
                    return json::json('failure', '停车费交付失败', $Array);
                }

            }

        }
    }

    public function Sum($id){
        $data    =DB::table('parking_fees')
            ->where('user_parkingfees',$id)
            ->sum('parking_money');
        return $data;
    }

    public function SelectFees(Request $request,$type){
        if($request ->isMethod('post')){
            $Array         = [];
            $user_id       = $request ->input('user_id');
            $data        = DB::table('parking_fees')
                ->select('parking_start','parking_end','parking_money','parking_set')
                ->where('user_parkingfees',$user_id)
                ->where('parking_state',$type)
                  ->orderby('parking_update_time','desc')
                ->get();
            foreach ($data as $value){
                /* $key = explode('-',$value ->order_goodsname);*/
                $key = $value ->parking_set;
                //$tmpArray[$key]['time'][] =array('time' =>$value->parking_start.$value->parking_end);
                $da[] = array('parking_start'=>$key,'parking_end'=>'','parking_money'=>$value->parking_money);
            }
            if($data == true){
                return json::json('success', '停车费获取成功', $da);
            }else{
                return json::json('failure', '停车费获取失败', $Array);
            }
        }
    }


}